/**
 * 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
 * 日    期：2020-03-25
 */
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Coupon;
import com.rzico.basics.entity.CouponCode;
import com.rzico.basics.entity.Member;
import com.rzico.basics.mapper.CouponCodeMapper;
import com.rzico.basics.mapper.CouponMapper;
import com.rzico.basics.model.CouponCodeVo;
import com.rzico.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 我的卡券业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */

@Service
public class CouponCodeService extends BaseServiceImpl<CouponCode, String> {

    @Autowired
    private CouponCodeMapper couponCodeMapper;

    @Autowired
    private CouponMapper couponMapper;

    @Override
    public BaseMapper<CouponCode, String> getMapper() {
        return couponCodeMapper;
    }

    public List<CouponCodeVo> selectVoList(Map<String, Object> params) {
        return couponCodeMapper.selectVoList(params);
    }

    public BigDecimal selectSumCount(Map<String, Object> params) {
        Map<String, BigDecimal> data = couponCodeMapper.selectSumCount(params);
        if (data != null && data.containsKey("balance")) {
            return new BigDecimal(data.get("balance").toString());
        } else {
            return BigDecimal.ZERO;
        }
    }

    public BigDecimal selectVirtualCount(Map<String, Object> params) {
        Map<String, BigDecimal> data = couponCodeMapper.selectSumCount(params);
        if (data != null && data.containsKey("stock")) {
            return new BigDecimal(data.get("stock").toString());
        } else {
            return BigDecimal.ZERO;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public CouponCode create(Coupon coupon, Member member, String sn, Integer quantity) {
        Date d = new Date();
        if (coupon.getBeginDate() != null && coupon.getBeginDate().after(new Date())) {
            d = coupon.getBeginDate();
        }
        int s = 0;
        if (quantity == 1) {
            CouponCode couponCode = createCouponCode(coupon, sn, s, member.getId());
            saveCouponCode(couponCode,coupon.getId());
            return couponCode;
        }

        int w = 0;
        for (int i = 0; i < quantity; i++) {
            w++;
            s++;
            CouponCode couponCode = createCouponCode(coupon, sn, s, member.getId());
            saveCouponCode(couponCode,coupon.getId());
        }
        return null;
    }

    private CouponCode createCouponCode(Coupon coupon, String sn, int s, Long memberId) {
        CouponCode couponCode = new CouponCode();
        couponCode.setDeleted(false);
        couponCode.setCouponId(coupon.getId());
//                couponCode.setAvailableStock(0L);
        couponCode.setBalance(1L);
        couponCode.setCreateDate(new Date());
        couponCode.setCode(sn + "-" + s);
        couponCode.setEnterpriseId(coupon.getEnterpriseId());
        couponCode.setIsUsed(false);
        couponCode.setMemberId(memberId);
        couponCode.setStock(1L);
//                if (coupon.getMaxgiven()!=null && coupon.getMaxgiven()>0) {
//                    d = DateUtils.truncate(d, Calendar.DATE);
//                    if (w>coupon.getMaxgiven()) {
//                        d = DateUtils.addMonths(d,1);
//                        w = 0;
//                    }
//                    couponCode.setStartDate(d);
//                    Date endDate = DateUtils.addSeconds(DateUtils.addMonths(d,1),-1);
//                    couponCode.setVaildDate(endDate);
//                } else {
        couponCode.setStartDate(coupon.getBeginDate());
        couponCode.setVaildDate(coupon.getEndDate());
//                }
        return couponCode;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveCouponCode(CouponCode couponCode,Long couponId){
        super.insertUseGeneratedKeys(couponCode);
        Map<String, Object> params = new HashMap<>();
        params.put("id", couponId);
        params.put("stock", 1);
        Integer updateRow = couponMapper.decStock(params);
        if (updateRow == 0) {
            throw new CustomException("已经领完了");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object[] ids) {
        int rw = 0;
        for (Object id : ids) {
            CouponCode couponCode = selectByPrimaryKey(id);
            couponCode.setDeleted(true);
            rw = rw + super.updateByPrimaryKeySelective(couponCode);
        }
        return rw;
    }

    public List<CouponCodeVo> getCouponCodeVoList(Long memberId) {
        Map<String, Object> params = new HashMap<>();
        params.put("memberId", memberId);
        return couponCodeMapper.selectVoList(params);
    }

    public List<CouponCodeVo> selectExchangeVoList(Long memberId, Long productId) {
        Map<String, Object> params = new HashMap<>();
        params.put("memberId", memberId);
        params.put("productId", productId);
        return couponCodeMapper.selectExchangeVoList(params);
    }

}
